<template>
  <div>
    <el-card>
      <template #header>
        <div class="card-header">
          <span>生产排产</span>
          <el-button type="primary" @click="handleRefresh">刷新</el-button>
        </div>
      </template>
      
      <el-table :data="scheduleList" style="width: 100%">
        <el-table-column prop="workOrderNo" label="工单号" />
        <el-table-column prop="product.name" label="产品名称" />
        <el-table-column prop="planQuantity" label="计划数量" />
        <el-table-column prop="priority" label="优先级" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" />
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getScheduleList } from '@/utils/api'

const scheduleList = ref([])

const loadData = async () => {
  try {
    const res = await getScheduleList()
    scheduleList.value = res.data
  } catch (error) {
    console.error('加载数据失败:', error)
  }
}

const getStatusType = (status) => {
  const map = {
    pending: 'info',
    running: 'primary',
    completed: 'success',
    stopped: 'danger'
  }
  return map[status] || 'info'
}

const getStatusText = (status) => {
  const map = {
    pending: '待生产',
    running: '生产中',
    completed: '已完成',
    stopped: '已停止'
  }
  return map[status] || status
}

const handleRefresh = () => {
  loadData()
}

onMounted(() => {
  loadData()
})
</script>
